﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;
using System.Web;
using System.Diagnostics.Contracts;
using System.ComponentModel.Composition;

namespace Kooboo.Commerce.Repositories
{
    [Export(typeof(IBrandRepository))]
    public partial class EntityRepositories:IBrandRepository
    {

        IQueryable<IBrand> _Brands;
        public IQueryable<IBrand> Brands
        {
            get
            {
                if (this._Brands == null)
                {
                    this._Brands = from brand in this.DataReaderContext.Brands
                                   select new QueryableBrand
                                   {
                                       Id = brand.Id,
                                       ProductTypes = this.DataReaderContext
                                       .BrandProductTypes
                                       .Where(i => i.BrandId == brand.Id)
                                       .Select(i => i.ProductType as IProductType),
                                       Description = brand.Description,
                                       Logo = brand.Logo,
                                       Name = brand.Name,
                                       Products = brand.Products,
                                   } as IBrand; 
                }

                return this._Brands;
            }
        }

        public void Post(IBrand brand)
        {
            Contract.Requires(brand != null);

            var item = new Brand();
            item.Name = brand.Name;
            item.Logo = brand.Logo;
            item.Description = brand.Description;

            this.DataWriterContext.Brands.InsertOnSubmit(item);

            foreach (var i in brand.ProductTypes)
            {
                var rel = new BrandProductType
                {
                    Brand = item,
                    ProductTypeId = i.Id
                };

                this.DataWriterContext.BrandProductTypes.InsertOnSubmit(rel);
            }

            this.DataWriterContext.SubmitChanges();


        }

        public void Put(IBrand brand)
        {
            Contract.Requires(brand != null);

            var item = this.DataWriterContext.Brands
                .Where(i => i.Id == brand.Id)
                .FirstOrDefault();

            item.Name = brand.Name;
            //item.Logo = brand.Logo;
            item.Description = brand.Description;

            //TODO: should move code to ui layer
            if (brand.Logo.Contains("-upload"))//update logo
            {
                string thumbnailDir = HttpContext.Current.Server.MapPath(@"~/images/brand/thumbnails/");
                string brandDir = HttpContext.Current.Server.MapPath(@"~/images/brand/");
                if (System.IO.File.Exists(thumbnailDir + brand.Logo))
                {
                    System.IO.File.Delete(thumbnailDir + item.Logo);
                    File.Move(thumbnailDir + brand.Logo, thumbnailDir + item.Logo);
                    System.IO.File.Delete(thumbnailDir + brand.Logo);
                }
                if (System.IO.File.Exists(brandDir + brand.Logo))
                {
                    System.IO.File.Delete(brandDir + item.Logo);
                    File.Move(brandDir + brand.Logo, brandDir + item.Logo);
                    System.IO.File.Delete(brandDir + brand.Logo);
                }
            }
            else
                item.Logo = brand.Logo;

            var rels = this.DataWriterContext.BrandProductTypes.Where(i => i.BrandId == brand.Id).ToList();

            foreach (var i in brand.ProductTypes)
            {
                var find = rels.Where(m => m.ProductTypeId == i.Id).FirstOrDefault();
                if (find == null)//new
                {
                    var rel = new BrandProductType
                    {
                        BrandId = brand.Id,
                        ProductTypeId = i.Id
                    };

                    this.DataWriterContext.BrandProductTypes.InsertOnSubmit(rel);
                }
                else
                {
                    rels.Remove(find);
                }

            }

            this.DataWriterContext.BrandProductTypes.DeleteAllOnSubmit(rels);


            this.DataWriterContext.SubmitChanges();
        }

        public void Delete(IBrand brand)
        {
            if (brand != null)
            {
                var item = this.DataWriterContext.Brands
                 .Where(i => i.Id == brand.Id)
                 .FirstOrDefault();

                if (item != null)
                {

                    var relations = this.DataWriterContext.BrandProductTypes.Where(i => i.BrandId == item.Id);

                    this.DataWriterContext.BrandProductTypes.DeleteAllOnSubmit(relations);
                    this.DataWriterContext.Brands.DeleteOnSubmit(item);

                    this.DataWriterContext.SubmitChanges();
                }
            }
        }

       
        public void Exclude(IBrand brand, IProductType productType)
        {
            var relation = this.DataWriterContext
                .BrandProductTypes
                .Where(i => i.BrandId == brand.Id && i.ProductTypeId == productType.Id)
                .FirstOrDefault();

            this.DataWriterContext.BrandProductTypes.DeleteOnSubmit(relation);

            this.DataWriterContext.SubmitChanges();
        }

        public void Include(IBrand brand, IProductType productType)
        {
            var relation = new BrandProductType
            {
                BrandId = brand.Id,
                ProductTypeId = productType.Id
            };

            this.DataWriterContext.BrandProductTypes.InsertOnSubmit(relation);
            this.DataWriterContext.SubmitChanges();
        }

      
    }


    public class QueryableBrand:Brand
    {
    }
}
